package com.etc.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.entity.Housing;
import com.etc.entity.Prices;
import com.etc.entity.Users;
import com.etc.entity.jsonui.JsonUi;
import com.etc.service.UsersService;
import com.etc.service.impl.UsersServiceImpl;
import com.etc.util.PageData;
import com.google.gson.Gson;

/**
 * Servlet implementation class UsersServlet
 */

/**
 * 后台管理界面用户展示
 * @author 李侨森
 *
 */



@WebServlet("/as.do")
public class AdminsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    private UsersService udao = new UsersServiceImpl();  

    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminsServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		//执行封装的分页查询
		String op= "query";
		if (request.getParameter("op")!=null) {
			op=request.getParameter("op");
		}
		if (op.equals("query")) {
			PageDoQuery(request, response);
		}
		//执行删除方法
		else if (op.equals("del")) {
			int id =Integer.valueOf(request.getParameter("id"));
			boolean flag= udao.DelUsers(id);
			//获取到Id后打印
			PrintWriter out = response.getWriter();
			out.print(flag);
			out.close();
		}
		else if (op.equals("update")) {
			//这时候用的是request.getReader();这个方法是用于获取页面一侧的ajax请求中的Json数据
		    BufferedReader reader= request.getReader();
		    //用字符串拼接的方式来拼接接收到的JSON数据
		    StringBuffer sBuffer = new StringBuffer();
		    //定义一个字符串让他为空，在下面的while循环中不断赋予它值，使得字符串可以拼接
		    String str = null;
		    while((str=reader.readLine())!=null) {
		    	sBuffer.append(str);
		    }
		    System.out.println(sBuffer);
		    //将GSON数据转化成Users对象，方便后面传入数据库
		    Gson gson = new Gson();
		    //转换对象的方式，并赋予users
		    Users users= gson.fromJson(sBuffer+"",Users.class);
		    System.out.println(users);
		    
//		    int id =Integer.parseInt(request.getParameter("id"));
		    
		    boolean flag= udao.UpdUsers(users);
		    PrintWriter out = response.getWriter();
			out.print(flag);
			out.close();
			System.out.println(flag);
		}else if(op.equals("add")) {
			String userName = request.getParameter("userName");
			String userPwd = request.getParameter("userPwd");
			String userSex = request.getParameter("userSex");
			String userTel = request.getParameter("userTel");
			String houseName = request.getParameter("houseName");
			String userDoorNO = request.getParameter("userDoorNO");

			int userState = Integer.parseInt(request.getParameter("userState"));
			Users users = new Users(userName, userPwd, userSex, userTel, houseName, userDoorNO,userState);
			boolean flag = udao.AddUsers(users);
			request.getRequestDispatcher("admin_users.jsp").forward(request, response);
			PrintWriter out = response.getWriter();
			out.print(flag);
			out.close();
		}
		else if(op.equals("removesession")) {
			//退出后清除所有的session信息
			request.getSession().invalidate();
			request.getRequestDispatcher("index.jsp").forward(request, response);

		}

	}
		//分页查询的方法封装
	private void PageDoQuery(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();
		int page = 1;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}

		
		int limit = 10;
		if (request.getParameter("limit") != null) {
			limit = Integer.parseInt(request.getParameter("limit"));
		}

		String strLike = "";
		if (request.getParameter("strLike") != null) {
			strLike = request.getParameter("strLike");
		}
		PageData<Users> pd = udao.GetUsersAll(page, limit, strLike);

		JsonUi<Users> jsonUi = new JsonUi<>(0, "", pd.getTotalCount(), pd.getData());

		Gson gson = new Gson();
		String string = gson.toJson(jsonUi);

		out.print(string);

		out.close();
	}
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
